/**
 * @PROJECT 
 * @DATE 2017-03-22 19:43:43
 * @AUTHOR LUYU
 */
package com.kool.tlwp.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.kool.tlwp.exception.AppException;

/**
 * @DESCRIBE
 * @AUTHOR LUYU
 * @DATE 2017-03-22 19:43:43
 *
 */
public class PropertyUtils {

	public static final Logger LOGGER = Logger.getLogger(PropertyUtils.class);

	public static Properties getSysPropFromRoot() throws IOException {
		FileInputStream fis = new FileInputStream(new File(StringUtils.getProjectRoot() + "\\tlwp.properties"));
		Properties prop = new Properties();
		prop.load(new InputStreamReader(fis, "UTF-8"));
		return prop;
	}

	public static Properties getSysPropFromSrc() throws IOException {
		InputStream input = PropertyUtils.class.getResourceAsStream("/tlwp.properties");
		Properties prop = new Properties();
		prop.load(new InputStreamReader(input, "UTF-8"));
		return prop;
	}

	public static String getDevMode() throws IOException {
		return getSysPropFromRoot().getProperty("DEV_MODE");
	}

	public static String getDevLog() throws IOException {
		return getSysPropFromRoot().getProperty("DEV_LOG");
	}

	public static String getSysValue(String key) throws IOException {
		return getSysPropFromRoot().getProperty(key);
	}

	public static String getValue(String propName, String key) throws AppException, IOException {
		return getProp(propName).getProperty(key);
	}

	public static Properties getProp(String propName) throws AppException, IOException {
		if (propName == null || propName.trim().length() <= 0) {
			throw new AppException("文件名不能为空");
		}
		if (propName.charAt(0) != '/') {
			propName = "/" + propName;
		}
		InputStream input = new FileInputStream(new File(StringUtils.getProjectRoot() + propName));
		Properties prop = new Properties();
		prop.load(input);
		return prop;

	}

	/**
	 * @DESCRIBE
	 * @DATE 2018年2月4日 下午6:29:40
	 *
	 * @param string
	 * @return
	 * @throws AppException
	 */
	public static String getValue(String key) throws AppException {
		String value = null;
		try {
			value = (String) getSysPropFromRoot().get(key);
		} catch (IOException e) {
			throw new AppException("取properties配置文件内容出错");
		}
		return value;
	}

}
